package com.example.piece.ui.activity

import android.Manifest
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.databinding.ViewDataBinding
import com.therouter.router.Autowired
import com.therouter.router.Route
import com.example.base.aop.click.RepeatClick
import com.example.base.aop.permission.PermissionRequest
import com.example.base.ext.navigationTo
import com.example.base.ui.fragment.BaseFragment
import com.example.piece.R
import com.example.piece.constant.Key
import com.example.piece.constant.RequestCode
import com.example.piece.constant.RoutePath
import com.example.piece.databinding.ActivityFeedbackBinding
import com.example.piece.entity.Picture
import com.example.piece.imageengine.GlideEngine
import com.example.piece.ui.adapter.AnnexAdapter
import com.example.piece.ui.adapter.HomeRecommendationAdapter
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener

/**
 *
 * 反馈意见
 */

@Route(path = RoutePath.MAIN_FEEDBACK)
class FeedbackActivity : Layer3Activity<ActivityFeedbackBinding, ViewDataBinding>() {
    override val title: String
        get() = H5_TITLE
    override val isTransparentStatusBar: Boolean = true
    override val isLockPortrait: Boolean = true
    override val statusBarAlpha: Int = 0
    override val isDarkMode: Boolean = true
    override val isFitsSystemWindows: Boolean = true
    override val leftIcon1Res: Int = R.drawable.ic_back

    override fun onClickLeftIcon1(view: View) {
        onBackPressed()

    }

    override val centerLayoutRes: Int
        get() = R.layout.activity_feedback
    private val adapter: AnnexAdapter by lazy {
        val adapter = AnnexAdapter(arrayListOf(), true, true, mCenterViewBinding?.rv)
        mCenterViewBinding?.rv?.adapter = adapter
        adapter
    }
    @JvmField
    @Autowired(name = Key.DataKey.H5_TITLE)
    var H5_TITLE: String = ""
    /**  点击添加图片的事件  **/
    @PermissionRequest(
        value = [
            Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO,
            Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.VIBRATE
        ]
    )
    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        adapter.bindAddClickListener {
            changePhoto()
        }
    }

    //更换头像
    fun changePhoto() {
        PictureSelector.create(this)
            .openGallery(SelectMimeType.ofImage())
            .setMaxSelectNum(adapter.maxSize-adapter.data.size)
            .setImageEngine(GlideEngine.createGlideEngine())
            .forResult(object : OnResultCallbackListener<LocalMedia?> {
                override fun onResult(result: ArrayList<LocalMedia?>?) {
                    if (result != null && result.size > 0) {
                        //  mCenterViewBinding?.imgHead?.setCircleCropImageUrl(result.get(0)?.availablePath)
                        for (it in result){
                            val picture =it?.availablePath?.let { Picture(it, it, false) }
                            picture?.let { adapter.addData(it) }

                            // 检测最大附件上传数
                            if (adapter.itemCount - 1 >= adapter.maxSize)
                                adapter.setShowAddItem(false)
                        }


                    }
                }

                override fun onCancel() {}
            })
    }
    private fun onAddPictureItem() {
        navigationTo(RoutePath.MAIN_CAMERA, RequestCode.REQUEST_RECORDING)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        adapter.processorActivityResult(requestCode, resultCode, data)
        super.onActivityResult(requestCode, resultCode, data)
    }

}